package com.ruoyi.system.mc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mc.entity.McProduct;
import com.ruoyi.system.mc.entity.McRoomPerson;
import com.ruoyi.system.mc.entity.R;
import com.ruoyi.system.mc.mapper.McRoomPersonMapper;
import com.ruoyi.system.mc.service.McRoomPersonService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/*
 *  @author 雨安
 *  类名： McRoomPersonServiceImpl
 *  创建时间：2024/5/31
 */
@Service
public class McRoomPersonServiceImpl extends ServiceImpl<McRoomPersonMapper, McRoomPerson> implements McRoomPersonService {

    //Service层
    @Resource
    private McRoomPersonService mcRoomPersonService;

    @Resource
    private SysUserMapper sysUserMapper;


    /**
     * 查询聊天列表
     * 第一步找到当前登录用户登录了的所有聊天室,通过聊天室找到 1 对 1 的聊天成员,通过聊天成员找到对应聊天的人的信息
     * @param mcRoomPerson 条件构造对象
     * @return 返回查询结果
     */
    @Override
    public R<McRoomPerson> selectAllRoom(McRoomPerson mcRoomPerson) {
        //页码
        Integer current = 0;
        //行数
        Integer size = 0;
        /**
         * 分页插件暂时没研究明白配置弄在哪
         */
        if (mcRoomPerson.getCurrentPage()!=0){
            //页码赋值
            current = (mcRoomPerson.getCurrentPage()-1)*mcRoomPerson.getPageSize();
            //总行数赋值
            size = mcRoomPerson.getPageSize();
        }
        //查到自己所有在的聊天室 IDs   .last("limit " + current + "," +size )
        List<Long> roomList = mcRoomPersonService.list(mcRoomPerson.getLambdaQueryWrapper(mcRoomPerson)).stream().map(McRoomPerson::getRoomId).collect(Collectors.toList());
        //判断自己所在的聊天不为null
        if (roomList.size() == 0){
            //返回空
            return null;
        }
        //查询自己所有对应的聊天对象
        List<McRoomPerson> mcRooList = mcRoomPersonService.list(new LambdaQueryWrapper<McRoomPerson>()
                //RoomID
                .in(McRoomPerson::getRoomId, roomList)
                //且用户ID不为自己
                .ne(McRoomPerson::getUserId, SecurityUtils.getUserId())
                //逻辑删除为假
                .eq(McRoomPerson::getDeleteFlag, Boolean.FALSE)
        );
        //遍历给对应用户设置名称
        for (McRoomPerson roomPerson : mcRooList) {
            //查询用户信息
            SysUser sysUser = sysUserMapper.selectUserById(roomPerson.getUserId());
            //设置返回商家名称
            roomPerson.setUserName(sysUser.getUserName());
            //设置返回的商家头像
            roomPerson.setAdminImg(sysUser.getAvatar());
        }
        //查询总行数
        Long total = mcRoomPersonService.count(new LambdaQueryWrapper<McRoomPerson>()
                //RoomID
                .in(McRoomPerson::getRoomId, roomList)
                //且用户ID不为自己
                .ne(McRoomPerson::getUserId, SecurityUtils.getUserId())
                //逻辑删除为假
                .eq(McRoomPerson::getDeleteFlag, Boolean.FALSE)
        );
        //返回统一返回格式类型
        return new R<>("查询成功",200,mcRooList,total,current,size);
    }



}
